﻿using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 打卡汇总_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        
        public DateTime KaiShiSJ = DateTime.Now;
        public DataTable table_DKHZJieGuo = new DataTable();
        public DataTable table_QingJiaShuRu1 = MiniExcel.QueryAsDataTable("C:\\输入\\请假.xlsx", useHeaderRow: true);
        public DataTable table_DaKaHuiZong = MiniExcel.QueryAsDataTable("C:\\输入\\打卡.xlsx", useHeaderRow: true);
        //public DataTable table_DaKaShuRu = table_DaKaHuiZong.Clone();
        public DataTable table_DaKaShuRu=new DataTable ();
        public DataTable table_QJSR = new DataTable();

        //public IEnumerable<IGrouping<string, DataRow>> RiQi = table_DaKaShuRu.Rows.Cast<DataRow>().GroupBy<DataRow, string>(dr => dr["日期"].ToString());
        public List<DateTime> rqs = new List<DateTime>();
        private void button1_Click(object sender, EventArgs e)
        {         
            DataColumn col1 = new DataColumn();
            col1.ColumnName = "姓名";
            table_DKHZJieGuo.Columns.Add(col1);
            DataColumn col2 = new DataColumn();
            col2.ColumnName = "部门";
            table_DKHZJieGuo.Columns.Add(col2);
            DataColumn col4 = new DataColumn();
            col4.ColumnName = "请假天数";
            table_DKHZJieGuo.Columns.Add(col4);
            DataColumn col5 = new DataColumn();
            col5.ColumnName = "早退次数";
            table_DKHZJieGuo.Columns.Add(col5);
            DataColumn col6 = new DataColumn();
            col6.ColumnName = "缺卡次数";
            table_DKHZJieGuo.Columns.Add(col6);
            DataColumn col7 = new DataColumn();
            col7.ColumnName = "设备异常次数";
            table_DKHZJieGuo.Columns.Add(col7);
            DataColumn col8 = new DataColumn();
            col8.ColumnName = "打卡地点异常次数";
            table_DKHZJieGuo.Columns.Add(col8);
            DataColumn col9 = new DataColumn();
            col9.ColumnName = "迟到次数";
            table_DKHZJieGuo.Columns.Add(col9);
            DataColumn col10 = new DataColumn();
            col10.ColumnName = "考勤";
            table_DKHZJieGuo.Columns.Add(col10);
            //DataTable table_DaKaShuRu = table_DaKaHuiZong.Clone();
            table_DaKaShuRu = table_DaKaHuiZong.Clone();
            foreach (DataRow row in table_DaKaHuiZong.Rows)
            {
                DataRow row2 = table_DaKaShuRu.NewRow();
                foreach (DataColumn column in table_DaKaHuiZong.Columns)
                {
                    row2[column.ColumnName] = row[column.ColumnName];
                }
                row2["日期"] = Convert.ToDateTime(row["日期"]).ToString("yyyy-MM-dd");
                table_DaKaShuRu.Rows.Add(row2);
            }
            table_QJSR = table_QingJiaShuRu1.Clone();
            foreach (DataRow row in table_QingJiaShuRu1.Rows)
            {
                DataRow row2 = table_QJSR.NewRow();
                foreach (DataColumn column in table_QingJiaShuRu1.Columns)
                {
                    row2[column.ColumnName] = row[column.ColumnName];
                }
                row2["开始时间"] = Convert.ToDateTime(row["开始时间"]);
                row2["结束时间"] = Convert.ToDateTime(row["结束时间"]).AddHours(12);
                table_QJSR.Rows.Add(row2);
            }
            IEnumerable<IGrouping<string, DataRow>> RiQi = table_DaKaShuRu.Rows.Cast<DataRow>().GroupBy<DataRow, string>(dr => dr["日期"].ToString());
            List<DateTime> rqs = new List<DateTime>();
            foreach (IGrouping<string, DataRow> ig in RiQi)
            {
                string ZBM = ig.Key.ToString();
                DateTime rq = Convert.ToDateTime(ZBM);
                rqs.Add(rq);

            }
            rqs = rqs.OrderBy(date => date).ToList();
            foreach (DateTime rq in rqs)
            {
                DataColumn col3 = new DataColumn();
                col3.ColumnName = rq.ToString("yyyy-MM-dd");
                table_DKHZJieGuo.Columns.Add(col3);
            }
            IEnumerable<IGrouping<string, DataRow>> XingMing = table_DaKaShuRu.Rows.Cast<DataRow>().GroupBy<DataRow, string>(dr => dr["姓名"].ToString());
            List<string> XMS = new List<string>();
            foreach (IGrouping<string, DataRow> ig in XingMing)
            {
                string XM = ig.Key.ToString();
                XMS.Add(XM);
            }
            for (int i = 0; i < XMS.Count; i++)
            {
                string XM = XMS[i].ToString();
                string XM2 = string.Empty;
                DataRow XMA = table_DKHZJieGuo.NewRow();
                XMA["姓名"] = XM;
                if (XM.Contains("（"))
                {
                    string str = XM;
                    string subStr = "（";

                    int position = str.IndexOf(subStr);

                    if (position != -1)
                    {
                        //Console.WriteLine($"子字符串 \"{subStr}\" 在字符串中的位置是: {position}");
                        XM2 = str.Substring(0, position);
                    }
                }
                else
                {
                    XM2 = XM;
                }
                DataRow[] rows = table_DaKaHuiZong.Select("姓名='" + XM + "'");
                XMA["部门"] = rows[0]["部门"].ToString();

                int ZTCS = 0;//早退次数
                int CDCS = 0;//迟到次数
                float QJCS = 0;//请假次数                                 
                int SBYCCS = 0;//设备异常次数
                int DKDDYCCS = 0;//打卡异常次数
                int QKCS = 0;//缺卡次数
                float ZCCQTS = 0;//正常出勤天数
                float KGTS = 0;//旷工天数
                foreach (DateTime rq in rqs)
                {
                    string JG = string.Empty;

                    DataRow[] SB = table_DaKaShuRu.Select("姓名='" + XM + "' and 日期  like '" + rq.ToString("yyyy-MM-dd") + "' and 打卡类型 = '上班'" + " and 打卡状态 not like '%失效%'");
                    Boolean SBQK = false;//上班缺卡
                    Boolean SBQJ = false;//上班请假
                    Boolean SBDK = false;//正常打卡
                    Boolean SBCD = false;//上班迟到
                    Boolean SBSBYC = false;//上班设备异常
                    Boolean SBDDYC = false;//上班地点异常
                    Boolean XBQK = false;//下班缺卡
                    Boolean XBQJ = false;// 下班请假
                    Boolean XBDK = false;//下班打卡
                    Boolean XBZT = false;//下班早退
                    Boolean XBSBYC = false;//下班设备异常
                    Boolean XBDDYC = false;//下班地点异常
                    if (SB.Count() == 0)
                    {
                        SBQK = true;
                        JG = JG + "未找到上班记录";
                    }
                    else
                    {
                        string SBRQ = SB[0]["日期"].ToString();
                        string SBDKZT = SB[0]["打卡状态"].ToString();
                        if (SB[0]["应打卡时间"].ToString() == "--")
                        {
                            JG = JG + "未指定考勤组";
                        }
                        else
                        {
                            string SBYDKSJ = rq.ToString("yyyy-MM-dd") + " " + SB[0]["应打卡时间"].ToString();
                            //找出申请人等于姓名  开始时间小于上班应打卡 审批状态通过 结束时间大于上班应打卡时间在请假申请表里
                            DataRow[] SP = table_QJSR.Select("申请人='" + XM2 + "' and 开始时间  <  '" + SBYDKSJ.ToString() + "' and 当前审批状态 = '已通过'" + " and 结束时间 > '" + SBYDKSJ.ToString() + "'");
                            //如果是包含正常打卡  在判断是不是这一天请假了却还打卡
                            if (SP.Count() > 0)
                            {
                                SBQJ = true;
                            }
                            else
                            {
                                if (SBDKZT.Contains("正常"))
                                {
                                    SBDK = true;

                                }
                                else if (SBDKZT.Contains("缺卡"))
                                {
                                    SBQK = true;
                                }
                                else if (SBDKZT.Contains("迟到"))
                                {
                                    SBCD = true;
                                }
                                if (SBDKZT.Contains("地点异常"))
                                {
                                    SBDDYC = true;
                                }
                                if (SBDKZT.Contains("设备异常"))
                                {
                                    SBSBYC = true;
                                }
                                if (SBDKZT.Contains("请假"))
                                {
                                    SBQJ = false;
                                }
                            }
                        }
                    }
                    //找出下班记录排除失效缺卡
                    DataRow[] XB = table_DaKaShuRu.Select("姓名='" + XM + "' and 日期  like '" + rq.ToString("yyyy-MM-dd") + "' and 打卡类型 = '下班'" + " and 打卡状态 not like '%失效%'");
                    if (XB.Count() == 0)
                    {
                        JG = JG + "未找到下班记录";
                        XBQK = true;
                    }
                    else
                    {
                        string XBRQ = XB[0]["日期"].ToString();
                        string XBDKZT = XB[0]["打卡状态"].ToString();
                        if (XB[0]["应打卡时间"].ToString() == "--")
                        {
                            JG = JG + "未指定考勤组";
                        }
                        else
                        {
                            string XBYDKSJ = rq.ToString("yyyy-MM-dd") + " " + XB[0]["应打卡时间"].ToString();
                            // //找出申请人等于姓名  开始时间小于上班应打卡 审批状态通过 结束时间大于下班应打卡时间在请假申请表里

                            DataRow[] SP = table_QJSR.Select("申请人='" + XM2 + "' and 开始时间  <  '" + XBYDKSJ.ToString() + "' and 当前审批状态 = '已通过'" + " and 结束时间 > '" + XBYDKSJ.ToString() + "'");
                            if (SP.Count() > 0)
                            {
                                XBQJ = true;
                            }
                            else
                            {
                                if (XBDKZT.Contains("正常"))
                                {
                                    XBDK = true;
                                }
                                else if (XBDKZT.Contains("缺卡"))
                                {
                                    XBQK = true;
                                }
                                else if (XBDKZT.Contains("早退"))
                                {
                                    XBZT = true;
                                }
                                if (XBDKZT.Contains("地点异常"))
                                {
                                    XBDDYC = true;
                                }
                                if (XBDKZT.Contains("设备异常"))
                                {
                                    XBSBYC = true;
                                }
                                if (XBDKZT.Contains("请假"))
                                {
                                    XBQJ = false;
                                }
                            }
                        }
                    }
                    //如果上班请假
                    if (SBQJ)
                    {
                        JG = JG + "上班：";
                        JG = JG + "请假";
                        QJCS = QJCS + 1;
                        //如果下班正常
                        if (XBDK)
                        {
                            JG = JG + "下班：";
                            JG = JG + "正常";
                            ZCCQTS = ZCCQTS + 1;
                        }
                        //如果下班请假
                        if (XBQJ)
                        {
                            JG = JG + "下班：";
                            JG = JG + "请假";
                            QJCS = QJCS + 1;
                        }
                        //如果下班缺卡
                        if (XBQK)
                        {
                            JG = JG + "下班：";
                            JG = JG + "缺卡";
                            QKCS = QKCS + 1;

                        }
                        //下班地点异常或下班设备异常下班早退
                        if (XBDDYC || XBSBYC || XBZT)
                        {
                            JG = JG + "下班：";
                            ZCCQTS = ZCCQTS + 1;
                            if (XBDDYC)
                            {
                                JG = JG + "地点异常";
                                DKDDYCCS = DKDDYCCS + 1;
                            }
                            if (XBSBYC)
                            {
                                JG = JG + "设备异常";
                                SBYCCS = SBYCCS + 1;
                            }
                            if (XBZT)
                            {
                                JG = JG + "早退";
                                ZTCS = ZTCS + 1;
                            }
                        }

                    }
                    //如果上班正常
                    else if (SBDK)
                    {
                        JG = JG + "上班：";
                        JG = JG + "正常";
                        ZCCQTS = ZCCQTS + 1;
                        //如果下班正常
                        if (XBDK)
                        {
                            JG = JG + "下班：";
                            JG = JG + "正常";
                            ZCCQTS = ZCCQTS + 1;
                        }
                        //如果下班请假
                        if (XBQJ)
                        {
                            JG = JG + "下班：";
                            JG = JG + "请假";
                            QJCS = QJCS + 1;
                        }
                        //如果下班缺卡
                        if (XBQK)
                        {
                            JG = JG + "下班：";
                            JG = JG + "缺卡";
                            ZCCQTS = ZCCQTS + 1;
                            QKCS = QKCS + 1;
                        }
                        //如果下班早退 地点异常 设备异常
                        if (XBDDYC || XBSBYC || XBZT)
                        {
                            JG = JG + "下班：";
                            ZCCQTS = ZCCQTS + 1;
                            if (XBDDYC)
                            {
                                JG = JG + "地点异常";
                                DKDDYCCS = DKDDYCCS + 1;
                            }
                            if (XBSBYC)
                            {
                                JG = JG + "设备异常";
                                SBYCCS = SBYCCS + 1;
                            }
                            if (XBZT)
                            {
                                JG = JG + "早退";
                                ZTCS = ZTCS + 1;
                            }
                        }
                    }
                    //如果上班迟到 或 地点异常 设备异常
                    else if (SBCD || SBSBYC || SBDDYC)
                    {
                        JG = JG + "上班：";
                        ZCCQTS = ZCCQTS + 1;
                        //上班迟到
                        if (SBCD)
                        {
                            JG = JG + "迟到";
                            CDCS = CDCS + 1;
                        }
                        //上班地点异常
                        if (SBDDYC)
                        {
                            JG = JG + "地点异常";
                            DKDDYCCS = DKDDYCCS + 1;
                        }
                        //上班设备异常
                        if (SBSBYC)
                        {
                            JG = JG + "设备异常";
                            SBYCCS = SBYCCS + 1;
                        }
                        //如果下班正常
                        if (XBDK)
                        {
                            JG = JG + "下班：";
                            JG = JG + "正常";
                            ZCCQTS = ZCCQTS + 1;
                        }
                        //如果下班请假
                        if (XBQJ)
                        {
                            JG = JG + "下班：";
                            JG = JG + "请假";
                            QJCS = QJCS + 1;
                        }
                        //如果下班缺卡
                        if (XBQK)
                        {
                            JG = JG + "下班：";
                            JG = JG + "缺卡";
                            QKCS = QKCS + 1;
                            ZCCQTS = ZCCQTS + 1;
                        }
                        //下班地点异常或下班设备异常下班早退
                        if (XBDDYC || XBSBYC || XBZT)
                        {
                            JG = JG + "下班：";
                            ZCCQTS = ZCCQTS + 1;
                            if (XBDDYC)
                            {
                                JG = JG + "地点异常";
                                DKDDYCCS = DKDDYCCS + 1;
                            }
                            if (XBSBYC)
                            {
                                JG = JG + "设备异常";
                                SBYCCS = SBYCCS + 1;
                            }
                            if (XBZT)
                            {
                                JG = JG + "早退";
                                ZTCS = ZTCS + 1;
                            }
                        }
                    }
                    //如果上班缺卡    ?下半夜缺卡不算考勤
                    else if (SBQK)
                    {
                        JG = JG + "上班：";
                        JG = JG + "缺卡";
                        QKCS = QKCS + 1;
                        ZCCQTS = ZCCQTS + 1;
                        //如果下班正常
                        if (XBDK)
                        {
                            JG = JG + "下班：";
                            JG = JG + "正常";
                            ZCCQTS = ZCCQTS + 1;
                        }
                        //如果下班请假
                        if (XBQJ)
                        {
                            JG = JG + "下班：";
                            JG = JG + "请假";
                            QJCS = QJCS + 1;

                        }
                        //如果下班缺卡
                        if (XBQK)
                        {
                            JG = JG + "下班：";
                            JG = JG + "缺卡";
                            QKCS = QKCS + 1;
                            ZCCQTS = ZCCQTS - 1;
                        }
                        //下班地点异常或下班设备异常下班早退
                        if (XBDDYC || XBSBYC || XBZT)
                        {
                            JG = JG + "下班：";
                            ZCCQTS = ZCCQTS + 1;
                            if (XBDDYC)
                            {
                                JG = JG + "地点异常";
                                DKDDYCCS = DKDDYCCS + 1;
                            }
                            if (XBSBYC)
                            {
                                JG = JG + "设备异常";
                                SBYCCS = SBYCCS + 1;
                            }
                            if (XBZT)
                            {
                                JG = JG + "早退";
                                ZTCS = ZTCS + 1;
                            }
                        }
                    }

                    XMA[rq.ToString("yyyy-MM-dd")] = JG;

                }
                XMA["迟到次数"] = CDCS;
                XMA["早退次数"] = ZTCS;
                XMA["请假天数"] = QJCS / 2;
                XMA["缺卡次数"] = QKCS;
                XMA["设备异常次数"] = SBYCCS;
                XMA["打卡地点异常次数"] = DKDDYCCS;
                XMA["考勤"] = ZCCQTS / 2;

                table_DKHZJieGuo.Rows.Add(XMA);

            }
            MiniExcel.SaveAs("C:\\输出\\考勤校验结果.xlsx", table_DKHZJieGuo);
            DateTime JSSJ = DateTime.Now;
            MessageBox.Show("开始时间；" + KaiShiSJ.ToString() + "结束时间：" + JSSJ.ToString());

        }
        private  void button2_Click(object sender, EventArgs e)
        {
            DateTime KaiShiSJ = DateTime.Now;
            //DataTable table_QingJiaShuRu1 = MiniExcel.QueryAsDataTable("C:\\输入\\请假.xlsx", useHeaderRow: true);
            //DataTable table_DaKaHuiZong = MiniExcel.QueryAsDataTable("C:\\输入\\打卡.xlsx", useHeaderRow: true);
             table_DKHZJieGuo = new DataTable();
            DataColumn col1 = new DataColumn();
            col1.ColumnName = "姓名";
            table_DKHZJieGuo.Columns.Add(col1);
            DataColumn col2 = new DataColumn();
            col2.ColumnName = "部门";
            table_DKHZJieGuo.Columns.Add(col2);
            DataColumn col4 = new DataColumn();
            col4.ColumnName = "请假天数";
            table_DKHZJieGuo.Columns.Add(col4);
            DataColumn col5 = new DataColumn();
            col5.ColumnName = "早退次数";
            table_DKHZJieGuo.Columns.Add(col5);
            DataColumn col6 = new DataColumn();
            col6.ColumnName = "缺卡次数";
            table_DKHZJieGuo.Columns.Add(col6);
            DataColumn col7 = new DataColumn();
            col7.ColumnName = "设备异常次数";
            table_DKHZJieGuo.Columns.Add(col7);
            DataColumn col8 = new DataColumn();
            col8.ColumnName = "打卡地点异常次数";
            table_DKHZJieGuo.Columns.Add(col8);
            DataColumn col9 = new DataColumn();
            col9.ColumnName = "迟到次数";
            table_DKHZJieGuo.Columns.Add(col9);
            DataColumn col10 = new DataColumn();
            col10.ColumnName = "考勤";
            table_DKHZJieGuo.Columns.Add(col10);
            table_DaKaShuRu = table_DaKaHuiZong.Clone();
            foreach (DataRow row in table_DaKaHuiZong.Rows)
            {
                DataRow row2 = table_DaKaShuRu.NewRow();
                foreach (DataColumn column in table_DaKaHuiZong.Columns)
                {
                    row2[column.ColumnName] = row[column.ColumnName];
                }
                row2["日期"] = Convert.ToDateTime(row["日期"]).ToString("yyyy-MM-dd");
                table_DaKaShuRu.Rows.Add(row2);
            }
            table_QJSR = table_QingJiaShuRu1.Clone();
            foreach (DataRow row in table_QingJiaShuRu1.Rows)
            {
                DataRow row2 = table_QJSR.NewRow();
                foreach (DataColumn column in table_QingJiaShuRu1.Columns)
                {
                    row2[column.ColumnName] = row[column.ColumnName];
                }
                row2["开始时间"] = Convert.ToDateTime(row["开始时间"]);
                row2["结束时间"] = Convert.ToDateTime(row["结束时间"]).AddHours(12);
                table_QJSR.Rows.Add(row2);
            }
            IEnumerable<IGrouping<string, DataRow>> RiQi = table_DaKaShuRu.Rows.Cast<DataRow>().GroupBy<DataRow, string>(dr => dr["日期"].ToString());
            //List<DateTime> rqs = new List<DateTime>();
            foreach (IGrouping<string, DataRow> ig in RiQi)
            {
                string ZBM = ig.Key.ToString();
                DateTime rq = Convert.ToDateTime(ZBM);
                rqs.Add(rq);

            }
            rqs = rqs.OrderBy(date => date).ToList();
            foreach (DateTime rq in rqs)
            {
                DataColumn col3 = new DataColumn();
                col3.ColumnName = rq.ToString("yyyy-MM-dd");
                table_DKHZJieGuo.Columns.Add(col3);
            }
            IEnumerable<IGrouping<string, DataRow>> XingMing = table_DaKaShuRu.Rows.Cast<DataRow>().GroupBy<DataRow, string>(dr => dr["姓名"].ToString());
            List<string> XMS = new List<string>();
            foreach (IGrouping<string, DataRow> ig in XingMing)
            {
                string XM = ig.Key.ToString();
                XMS.Add(XM);
            }
            int XingMingShuLiang = XMS.Count();
            //            int 线程数 = 10；
            //倍数 = 不重复的人名数 / 10;
            //            for (int j = 0; j < 倍数；j++)
            int XianChengShu = 4;
            int BeiShu = XingMingShuLiang / 4;
            for (int j = 0; j < BeiShu; j++)
            {
                Task[] XianChengChi1 = new Task[XianChengShu];
                //               task[] 线程池 = new task[线程数];
                for (int i = XianChengShu * j; i < XianChengShu * (j + 1); i++)
                {
                    Thread.Sleep(100);
                    string XM = XMS[i].ToString();
                    Task t = Task.Run(async ()=>
                    {
                        DataRow row1= table_DKHZJieGuo.NewRow();
                         row1= await KaoQinHuiZong(XM);
                        lock (table_DKHZJieGuo)
                        {
                            table_DKHZJieGuo.Rows.Add(row1);
                        }
                    });
                   
                    //线程池的第【i-线程数*j]=线程
                    XianChengChi1[i - XianChengShu * j] = t;
                }
                Task.WaitAll(XianChengChi1);
            }
            //Task[] 线程池2 = new Task[chbms.Count - beishu * xianchengshu];
            //for (k = 倍数 * 线程数; i < 总数；i++)
            Task[] XianChengChi2 = new Task[XMS.Count - BeiShu * XianChengShu];
            for (int k = BeiShu * XianChengShu; k < XingMingShuLiang; k++)
            {
                //if (k >= XingMingShuLiang)
                //{
                //    continue;
                //}
                //Task[] XianChengChi1 = new Task[XianChengShu];
                Thread.Sleep(100);
                string XM = XMS[k].ToString();
                Task t = Task.Run(async ()=>
                {
                    DataRow row2 = table_DKHZJieGuo.NewRow();
                     row2 = await KaoQinHuiZong(XM);
                    lock (table_DKHZJieGuo)
                    {
                        table_DKHZJieGuo.Rows.Add(row2);
                    }
                });
               
                ////线程池的第【i-线程数*j]=线程
                XianChengChi2[k - XianChengShu * BeiShu] = t;
                //for (int i = XianChengShu * j; i < XianChengShu * (j + 1); i++)
                //{

                //}

            }

            Task.WaitAll(XianChengChi2);
            .MiniExcel.SaveAs("C:\\输出\\考勤校验结果.xlsx", table_DKHZJieGuo);
            DateTime JSSJ = DateTime.Now;
            MessageBox.Show("开始时间；" + KaiShiSJ.ToString() + "结束时间：" + JSSJ.ToString());


        }
        public async Task<DataRow> KaoQinHuiZong(string name)
        {
            string XM = name;
            string XM2 = string.Empty;
            DataRow XMA = table_DKHZJieGuo.NewRow();
            XMA["姓名"] = XM;
            if (XM.Contains("（"))
            {
                string str = XM;
                string subStr = "（";

                int position = str.IndexOf(subStr);

                if (position != -1)
                {
                    //Console.WriteLine($"子字符串 \"{subStr}\" 在字符串中的位置是: {position}");
                    XM2 = str.Substring(0, position);
                }
            }
            else
            {
                XM2 = XM;
            }
            DataRow[] rows = table_DaKaHuiZong.Select("姓名='" + XM + "'");
            XMA["部门"] = rows[0]["部门"].ToString();

            int ZTCS = 0;//早退次数
            int CDCS = 0;//迟到次数
            float QJCS = 0;//请假次数                                 
            int SBYCCS = 0;//设备异常次数
            int DKDDYCCS = 0;//打卡异常次数
            int QKCS = 0;//缺卡次数
            float ZCCQTS = 0;//正常出勤天数
            float KGTS = 0;//旷工天数
            foreach (DateTime rq in rqs)
            {
                string JG = string.Empty;

                DataRow[] SB = table_DaKaShuRu.Select("姓名='" + XM + "' and 日期  like '" + rq.ToString("yyyy-MM-dd") + "' and 打卡类型 = '上班'" + " and 打卡状态 not like '%失效%'");
                Boolean SBQK = false;//上班缺卡
                Boolean SBQJ = false;//上班请假
                Boolean SBDK = false;//正常打卡
                Boolean SBCD = false;//上班迟到
                Boolean SBSBYC = false;//上班设备异常
                Boolean SBDDYC = false;//上班地点异常
                Boolean XBQK = false;//下班缺卡
                Boolean XBQJ = false;// 下班请假
                Boolean XBDK = false;//下班打卡
                Boolean XBZT = false;//下班早退
                Boolean XBSBYC = false;//下班设备异常
                Boolean XBDDYC = false;//下班地点异常
                if (SB.Count() == 0)
                {
                    SBQK = true;
                    JG = JG + "未找到上班记录";
                }
                else
                {
                    string SBRQ = SB[0]["日期"].ToString();
                    string SBDKZT = SB[0]["打卡状态"].ToString();
                    if (SB[0]["应打卡时间"].ToString() == "--")
                    {
                        JG = JG + "未指定考勤组";
                    }
                    else
                    {
                        string SBYDKSJ = rq.ToString("yyyy-MM-dd") + " " + SB[0]["应打卡时间"].ToString();
                        //找出申请人等于姓名  开始时间小于上班应打卡 审批状态通过 结束时间大于上班应打卡时间在请假申请表里
                        DataRow[] SP = table_QJSR.Select("申请人='" + XM2 + "' and 开始时间  <  '" + SBYDKSJ.ToString() + "' and 当前审批状态 = '已通过'" + " and 结束时间 > '" + SBYDKSJ.ToString() + "'");
                        //如果是包含正常打卡  在判断是不是这一天请假了却还打卡
                        if (SP.Count() > 0)
                        {
                            SBQJ = true;
                        }
                        else
                        {
                            if (SBDKZT.Contains("正常"))
                            {
                                SBDK = true;

                            }
                            else if (SBDKZT.Contains("缺卡"))
                            {
                                SBQK = true;
                            }
                            else if (SBDKZT.Contains("迟到"))
                            {
                                SBCD = true;
                            }
                            if (SBDKZT.Contains("地点异常"))
                            {
                                SBDDYC = true;
                            }
                            if (SBDKZT.Contains("设备异常"))
                            {
                                SBSBYC = true;
                            }
                            if (SBDKZT.Contains("请假"))
                            {
                                SBQJ = false;
                            }
                        }
                    }
                }
                //找出下班记录排除失效缺卡
                DataRow[] XB = table_DaKaShuRu.Select("姓名='" + XM + "' and 日期  like '" + rq.ToString("yyyy-MM-dd") + "' and 打卡类型 = '下班'" + " and 打卡状态 not like '%失效%'");
                if (XB.Count() == 0)
                {
                    JG = JG + "未找到下班记录";
                    XBQK = true;
                }
                else
                {
                    string XBRQ = XB[0]["日期"].ToString();
                    string XBDKZT = XB[0]["打卡状态"].ToString();
                    if (XB[0]["应打卡时间"].ToString() == "--")
                    {
                        JG = JG + "未指定考勤组";
                    }
                    else
                    {
                        string XBYDKSJ = rq.ToString("yyyy-MM-dd") + " " + XB[0]["应打卡时间"].ToString();
                        // //找出申请人等于姓名  开始时间小于上班应打卡 审批状态通过 结束时间大于下班应打卡时间在请假申请表里

                        DataRow[] SP = table_QJSR.Select("申请人='" + XM2 + "' and 开始时间  <  '" + XBYDKSJ.ToString() + "' and 当前审批状态 = '已通过'" + " and 结束时间 > '" + XBYDKSJ.ToString() + "'");
                        if (SP.Count() > 0)
                        {
                            XBQJ = true;
                        }
                        else
                        {
                            if (XBDKZT.Contains("正常"))
                            {
                                XBDK = true;
                            }
                            else if (XBDKZT.Contains("缺卡"))
                            {
                                XBQK = true;
                            }
                            else if (XBDKZT.Contains("早退"))
                            {
                                XBZT = true;
                            }
                            if (XBDKZT.Contains("地点异常"))
                            {
                                XBDDYC = true;
                            }
                            if (XBDKZT.Contains("设备异常"))
                            {
                                XBSBYC = true;
                            }
                            if (XBDKZT.Contains("请假"))
                            {
                                XBQJ = false;
                            }
                        }
                    }
                }
                //如果上班请假
                if (SBQJ)
                {
                    JG = JG + "上班：";
                    JG = JG + "请假";
                    QJCS = QJCS + 1;
                    //如果下班正常
                    if (XBDK)
                    {
                        JG = JG + "下班：";
                        JG = JG + "正常";
                        ZCCQTS = ZCCQTS + 1;
                    }
                    //如果下班请假
                    if (XBQJ)
                    {
                        JG = JG + "下班：";
                        JG = JG + "请假";
                        QJCS = QJCS + 1;
                    }
                    //如果下班缺卡
                    if (XBQK)
                    {
                        JG = JG + "下班：";
                        JG = JG + "缺卡";
                        QKCS = QKCS + 1;

                    }
                    //下班地点异常或下班设备异常下班早退
                    if (XBDDYC || XBSBYC || XBZT)
                    {
                        JG = JG + "下班：";
                        ZCCQTS = ZCCQTS + 1;
                        if (XBDDYC)
                        {
                            JG = JG + "地点异常";
                            DKDDYCCS = DKDDYCCS + 1;
                        }
                        if (XBSBYC)
                        {
                            JG = JG + "设备异常";
                            SBYCCS = SBYCCS + 1;
                        }
                        if (XBZT)
                        {
                            JG = JG + "早退";
                            ZTCS = ZTCS + 1;
                        }
                    }

                }
                //如果上班正常
                else if (SBDK)
                {
                    JG = JG + "上班：";
                    JG = JG + "正常";
                    ZCCQTS = ZCCQTS + 1;
                    //如果下班正常
                    if (XBDK)
                    {
                        JG = JG + "下班：";
                        JG = JG + "正常";
                        ZCCQTS = ZCCQTS + 1;
                    }
                    //如果下班请假
                    if (XBQJ)
                    {
                        JG = JG + "下班：";
                        JG = JG + "请假";
                        QJCS = QJCS + 1;
                    }
                    //如果下班缺卡
                    if (XBQK)
                    {
                        JG = JG + "下班：";
                        JG = JG + "缺卡";
                        ZCCQTS = ZCCQTS + 1;
                        QKCS = QKCS + 1;
                    }
                    //如果下班早退 地点异常 设备异常
                    if (XBDDYC || XBSBYC || XBZT)
                    {
                        JG = JG + "下班：";
                        ZCCQTS = ZCCQTS + 1;
                        if (XBDDYC)
                        {
                            JG = JG + "地点异常";
                            DKDDYCCS = DKDDYCCS + 1;
                        }
                        if (XBSBYC)
                        {
                            JG = JG + "设备异常";
                            SBYCCS = SBYCCS + 1;
                        }
                        if (XBZT)
                        {
                            JG = JG + "早退";
                            ZTCS = ZTCS + 1;
                        }
                    }
                }
                //如果上班迟到 或 地点异常 设备异常
                else if (SBCD || SBSBYC || SBDDYC)
                {
                    JG = JG + "上班：";
                    ZCCQTS = ZCCQTS + 1;
                    //上班迟到
                    if (SBCD)
                    {
                        JG = JG + "迟到";
                        CDCS = CDCS + 1;
                    }
                    //上班地点异常
                    if (SBDDYC)
                    {
                        JG = JG + "地点异常";
                        DKDDYCCS = DKDDYCCS + 1;
                    }
                    //上班设备异常
                    if (SBSBYC)
                    {
                        JG = JG + "设备异常";
                        SBYCCS = SBYCCS + 1;
                    }
                    //如果下班正常
                    if (XBDK)
                    {
                        JG = JG + "下班：";
                        JG = JG + "正常";
                        ZCCQTS = ZCCQTS + 1;
                    }
                    //如果下班请假
                    if (XBQJ)
                    {
                        JG = JG + "下班：";
                        JG = JG + "请假";
                        QJCS = QJCS + 1;
                    }
                    //如果下班缺卡
                    if (XBQK)
                    {
                        JG = JG + "下班：";
                        JG = JG + "缺卡";
                        QKCS = QKCS + 1;
                        ZCCQTS = ZCCQTS + 1;
                    }
                    //下班地点异常或下班设备异常下班早退
                    if (XBDDYC || XBSBYC || XBZT)
                    {
                        JG = JG + "下班：";
                        ZCCQTS = ZCCQTS + 1;
                        if (XBDDYC)
                        {
                            JG = JG + "地点异常";
                            DKDDYCCS = DKDDYCCS + 1;
                        }
                        if (XBSBYC)
                        {
                            JG = JG + "设备异常";
                            SBYCCS = SBYCCS + 1;
                        }
                        if (XBZT)
                        {
                            JG = JG + "早退";
                            ZTCS = ZTCS + 1;
                        }
                    }
                }
                //如果上班缺卡    ?下半夜缺卡不算考勤
                else if (SBQK)
                {
                    JG = JG + "上班：";
                    JG = JG + "缺卡";
                    QKCS = QKCS + 1;
                    ZCCQTS = ZCCQTS + 1;
                    //如果下班正常
                    if (XBDK)
                    {
                        JG = JG + "下班：";
                        JG = JG + "正常";
                        ZCCQTS = ZCCQTS + 1;
                    }
                    //如果下班请假
                    if (XBQJ)
                    {
                        JG = JG + "下班：";
                        JG = JG + "请假";
                        QJCS = QJCS + 1;

                    }
                    //如果下班缺卡
                    if (XBQK)
                    {
                        JG = JG + "下班：";
                        JG = JG + "缺卡";
                        QKCS = QKCS + 1;
                        ZCCQTS = ZCCQTS - 1;
                    }
                    //下班地点异常或下班设备异常下班早退
                    if (XBDDYC || XBSBYC || XBZT)
                    {
                        JG = JG + "下班：";
                        ZCCQTS = ZCCQTS + 1;
                        if (XBDDYC)
                        {
                            JG = JG + "地点异常";
                            DKDDYCCS = DKDDYCCS + 1;
                        }
                        if (XBSBYC)
                        {
                            JG = JG + "设备异常";
                            SBYCCS = SBYCCS + 1;
                        }
                        if (XBZT)
                        {
                            JG = JG + "早退";
                            ZTCS = ZTCS + 1;
                        }
                    }
                }

                XMA[rq.ToString("yyyy-MM-dd")] = JG;

            }
            XMA["迟到次数"] = CDCS;
            XMA["早退次数"] = ZTCS;
            XMA["请假天数"] = QJCS / 2;
            XMA["缺卡次数"] = QKCS;
            XMA["设备异常次数"] = SBYCCS;
            XMA["打卡地点异常次数"] = DKDDYCCS;
            XMA["考勤"] = ZCCQTS / 2;
            return XMA;
            //lock(table_DKHZJieGuo.Rows)
            //{
            //    table_DKHZJieGuo.Rows.Add(XMA);
            //}
            

        }








    }
}

